package com.kele.controller;

import com.kele.domain.Blog;
import com.kele.domain.Sentence;
import com.kele.service.BlogService;
import com.kele.service.SentenceService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author 赵奥林
 * @date 2021年06月06日 15:01
 * @Description
 */
@RequestMapping("/find")
@Controller
public class FindAllController {

    @Resource
    private BlogService blogService;

    @Resource
    private SentenceService sentenceService;


    @RequestMapping("/showAll")
    public String showAll(Model model){

        Random random=new Random();
        /**
         * 显示博客内容
         */
        List<Blog> blogs = blogService.showBlogs();
        List<Blog> list=new ArrayList<Blog>();
        //随机展示博客
        for (int i = 0; i < 6; i++) {
            int intRandom=random.nextInt(blogs.size()-1);
            list.add(blogs.get(intRandom));
            blogs.remove(blogs.get(intRandom));
        }
        model.addAttribute("blogs",list);


        /**
         * 显示语句内容
         */
        List<Sentence> sentences = sentenceService.showSentence();
        List<Sentence> list2 = new ArrayList<Sentence>();

        for (int i = 0; i < 5; i++) {
            int intRandom2 = random.nextInt(sentences.size() - 1);
            list2.add(sentences.get(intRandom2));
            sentences.remove(sentences.get(intRandom2));
        }
        model.addAttribute("sentence",sentences);

        return "static_resources/templates/index";
    }
}
